﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class CountriesController : Controller
    {
        private atletisEntities db = new atletisEntities();

        //
        // GET: /Countries/

        public ViewResult Index(string searchstring)
        {
            var countries = db.Countries.OrderBy(c => c.Name).ToList();

            if (!string.IsNullOrEmpty(searchstring))
            {
                countries = countries.Where(c => c.Name.Contains(searchstring)).ToList();
            }
            
            return View(countries);
        }

        //
        // GET: /Countries/Details/5

        public ViewResult Details(string id)
        {
            Countries countries = db.Countries.Single(c => c.ID == id);
            return View(countries);
        }

        //
        // GET: /Countries/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Countries/Create

        [HttpPost]
        public ActionResult Create(Countries countries)
        {
            countries.Modified = DateTime.Now;
            countries.Sync = true;
            
            if (ModelState.IsValid)
            {
                db.Countries.AddObject(countries);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(countries);
        }
        
        //
        // GET: /Countries/Edit/5
 
        public ActionResult Edit(string id)
        {
            Countries countries = db.Countries.Single(c => c.ID == id);
            return View(countries);
        }

        //
        // POST: /Countries/Edit/5

        [HttpPost]
        public ActionResult Edit(Countries countries)
        {
            countries.Modified = DateTime.Now;
            countries.Sync = true;
            
            if (ModelState.IsValid)
            {
                db.Countries.Attach(countries);
                db.ObjectStateManager.ChangeObjectState(countries, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(countries);
        }

        //
        // GET: /Countries/Delete/5
 
        public ActionResult Delete(string id)
        {
            Countries countries = db.Countries.Single(c => c.ID == id);
            return View(countries);
        }

        //
        // POST: /Countries/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            Countries countries = db.Countries.Single(c => c.ID == id);
            db.Countries.DeleteObject(countries);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}